all files / components/ LocalStorageComponent.react.js

86.89% Statements 53/61
69.39% Branches 34/49
90.91% Functions 10/11
60% Lines 12/20
4 statements, 13 branches Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74                                                                                                                              
import React, {Component} from 'react';
import PropTypes from 'prop-types';
 
/**
 * LocalStorageComponent is an example component.
 * It takes a property, `label`, and
 * displays it.
 * It renders an input with the property `value`
 * which is editable by the user.
 */
export default class LocalStorageComponent extends Component {
    constructor(props) {
        super(props);
        const {setProps, label, value} = this.props;
        Eif (value) {
            localStorage.setItem(label, value);
        } else {
            if(!localStorage.getItem(label)) {
                localStorage.setItem(label, 0);
            }
        }
        window.addEventListener('storage', function (event) {
            if (event.key == label) {
                if (setProps && (event.oldValue != event.newValue)) {
                    setProps({value: event.newValue});
                }
 
            }
        });
        
 
    }
    render() {
        const {id, setProps, label, value} = this.props;
        Eif (value) {
            localStorage.setItem(label, value);
        } else {
            let last_value = localStorage.getItem(label);
            if (setProps && last_value != value) {
                setProps({value: last_value});
            }
        }
        return (
            <div id={id}>
                { localStorage.getItem(label)}
            </div>
        );
    }
}
 
LocalStorageComponent.propTypes = {
    /**
     * The ID used to identify this compnent in Dash callbacks
     */
    id: PropTypes.string,
 
    /**
     * The label to store value
     */
    label: PropTypes.string,
 
    /**
     * The value displayed in the input
     */
    value: PropTypes.string,
 
    /**
     * Dash-assigned callback that should be called whenever any of the
     * properties change
     */
    setProps: PropTypes.func
};